package com.megvii.koala.constance;

import android.content.Context;

import com.megvii.koala.R;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * Created by linyue on 15/12/21.
 */
public class ServerErrorCode {
    public static final int NO_ERROR = 0;

    ////////////////    code from server    ////////////////
    public static final int ERROR_UNKNOWN = -1000;

    public static final int ERROR_METHOD_ERROR = -1001;

    public static final int ERROR_INVALID_PARAM = -1002;

    public static final int ERROR_DB_ERROR = -1003;

    public static final int ERROR_NOT_ALLOWED = -1004;

    public static final int ERROR_NETWORK_ERROR = -1005;

    public static final int ERROR_INVALID_THEME = -1006;

    public static final int ERROR_PASSWORD_NEED_CHANGE = -1007;

    public static final int ERROR_VERIFICATION_CODE = -1008;

    //common
    public static final int ERROR_USER_NOT_EXIST = -2000;

    public static final int ERROR_GROUP_NOT_EXIST = -2001;

    public static final int ERROR_GROUP_ALREADY_EXIST = -2002;

    public static final int ERROR_CAMERA_NOT_EXIST = -2003;

    public static final int ERROR_CORE_NOT_EXIST = -2004;

    public static final int ERROR_SUBJECT_NOT_EXIST = -2005;

    public static final int ERROR_PHOTO_NOT_EXIST = -2006;

    public static final int ERROR_BOX_NOT_EXIST = -2007;

    public static final int ERROR_SCREEN_NOT_EXIST = -2008;

    public static final int ERROR_COMPANY_NOT_EXIST = -2009;

    public static final int ERROR_EVENT_NOT_EXIST = -2010;

    public static final int ERROR_USERNAME_EXISTED = -2011;

    public static final int ERROR_BOX_EXISTED = -20011;

    public static final int ERROR_SUBJECT_PHOTO_FULL = -2012;

    public static final int ERROR_EMAIL_EXISTED = -2015;

    public static final int ERROR_EXCEL_INVALID = -2013;

    public static final int ERROR_FILE_NOT_EXIST = -2014;

    //auth
    public static final int ERROR_PASSWORD_ERROR = -3000;

    public static final int ERROR_CAPTCHAS_ERROR = -3001;

    public static final int ERROR_PASSWORD_INCONSISTENT = -3002;

    public static final int ERROR_NEED_LOGIN = -3003;

    public static final int ERROR_WEATHER_ERROR = -4001;

    public static final int ERROR_FACE_NO_DETECTED = -5000;

    public static final int ERROR_CAMERA_CONFIG_INCORRECT = -5001;

    public static final int ERROR_JSON_INVALID = -5002;

    public static final int ERROR_DUPLICATED = -5003;

    public static final int ERROR_FACE_QUALITY_LOW = -5004;

    private static HashMap<Integer, String> stringMap = new HashMap<>();

    public static void init(Context context) {
        ServerErrorCode.stringMap = getMapping(ServerErrorCode.class, R.string.class, context);
    }

    public static HashMap<Integer, String> getMapping(Class<?> constanceClass, Class<?> rClass,
                                                      Context context) {
        HashMap<Integer, String> result = new HashMap<>();
        Field[] constantFields = constanceClass.getFields();
        Field[] resourceFields = rClass.getFields();
        HashMap<String, String> fieldNameToResValue = new HashMap<>();
        for (Field field : resourceFields) {
            String fieldName = field.getName();
            try {
                if (fieldName.equals("$change")) continue;
                int id = field.getInt(null);
                String resString = context.getString(id);
                fieldNameToResValue.put(fieldName, resString);
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
        for (Field field : constantFields) {
            String fieldName = field.getName();
            String resValue = fieldNameToResValue.get(fieldName);
            if (resValue != null) {
                try {
                    int errorCode = field.getInt(null);
                    result.put(errorCode, resValue);
                } catch (IllegalAccessException e) {
                    //e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static boolean isDescriptionExist(int code) {
        return ServerErrorCode.stringMap.containsKey(code);
    }

    public static String getDescription(int code) {
        String str = ServerErrorCode.stringMap.get(code);
        if (str == null) {
            return "ERROR " + code;
        } else {
            return str;
        }
    }
}
